﻿using ASPNETMVC4Tutorial.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ASPNETMVC4Tutorial.Controllers
{
    public class ProductsController : Controller
    {
        //
        // GET: /HelloWorld/

        public ActionResult Index()
        {
            return View(); 
        }

       private readonly IProductRepository repository;

       public ProductsController(IProductRepository repository)
        {
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }
            this.repository = repository;
        }


        public string Welcome(string name, int numTimes = 1)
        {
            return HttpUtility.HtmlEncode("Hi " + name + ", NumTimes is: " + numTimes);   

        }


        //Get GetProductById?id=2
        public ActionResult GetProductById(int id)
        {

            Product product = repository.Get(id);
            return View(product);
        }


        #region How to pass data from Controller to View
        public ActionResult IndexNotStonglyTyped()
        {
            return View(repository.GetAll());
        }

        //http://blogs.msdn.com/b/rickandy/archive/2011/01/28/dynamic-v-strongly-typed-views.aspx
        public ActionResult StonglyTypedIndex()
        {
            return View(repository.GetAll());
        }

        public ActionResult IndexViewBag()
        {
            ViewBag.Products = repository.GetAll();
            return View();
        }




        #endregion


    }
}
